package com.becca.cake.core.excel.excelReader;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;

import jxl.Sheet;
import jxl.Workbook;
import jxl.read.biff.BiffException;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.becca.cake.core.common.BaseAction;

/**
 * 通用JXL导入EXCEL的方法
 * @copyright  尊星(上海)信息科技有限公司
 * @author hejinguo
 * @date 2016年9月2日 下午6:01:39
 */
public class ExcelReaderByJxlUtil extends BaseAction {
    private static Log         logger         = LogFactory.getLog(ExcelReaderByJxlUtil.class);
    public static final String READEXCELERROE = "在读取Excel文件中的方法出现错误!";                        //上传文件目录

    /**
     * @Title getWorkBook  
     * 根据 InputStream获得工作薄对象
     * @param ins 
     * @param 设定文件 
     * @return Workbook  返回类型 
     * @throws
     */
    public Workbook getWorkBook(InputStream ins) {
        Workbook book = null;
        try {
            book = Workbook.getWorkbook(ins);
        } catch (BiffException e) {
            if (logger.isErrorEnabled()) {
                logger.error("根据 InputStream获得工作薄对象时BiffException流异常!", e);
            }
        } catch (IOException e) {
            if (logger.isErrorEnabled()) {
                logger.error("根据 InputStream获得工作薄对象时IO流异常!", e);
            }
        }
        return book;
    }

    /**
     * @Title getWorkBook  
     * 根据EXCEL文件路径获得工作薄对象
     * @param fileName
     * @param 设定文件 
     * @return Workbook  返回类型 
     * @throws
     */
    public Workbook getWorkBook(String fileName) {
        File file = null;
        InputStream is = null;
        if (StringUtils.isNotEmpty(fileName)) {
            return null;
        }

        file = new File(fileName);
        if (!file.exists()) {
            return null;
        }
        try {
            is = new FileInputStream(file);
        } catch (FileNotFoundException e) {
            if (logger.isErrorEnabled()) {
                logger.error("根据EXCEL文件路径获得工作薄对象时没有发现文件异常!", e);
            }
        }

        return this.getWorkBook(is);
    }

    /**
     * 赠送积分中读取数据
     * @author hejinguo
     * @return
     */
    public String readExcelToPresentScore(InputStream stream) {
        String result = "";
        Workbook book = this.getWorkBook(stream);
        Sheet sheet = book.getSheet(0); // 获得第一个工作表对象  
        int rows = sheet.getRows();
        int columns = sheet.getColumns();
        String returnStr = "";
        int m = 0;
        Object obj = null;
        for (int i = 0; i < rows; i++) {
            for (int j = 0; j < columns; j++) {
                obj = sheet.getCell(j, i).getContents();
                if (obj != "" && obj != null) {
                    if (m > 0) {
                        returnStr += "," + obj.toString();
                    } else {
                        returnStr = obj.toString();
                        m++;
                    }
                }
            }
        }
        result = "{success:true,userStr:'" + returnStr + "'}";
        return result;
    }

}
